import 'dart:convert' as convert;

import 'package:flutter/material.dart';
import 'package:village_android/component/icon_tile.dart';
import 'package:village_android/component/jd_button.dart';
import 'package:village_android/component/member_header.dart';
import 'package:village_android/config/contants.dart';
import 'package:village_android/main.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/profile/profile_detail.dart';
import 'package:village_android/pages/profile/profile_verify_detail.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/store_utils.dart';

class ProfilePage extends StatefulWidget {
  UserModel userModel;
  var userId = sp.get(K_USER_ID);

  ProfilePage({this.userModel});

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text("个人中心"),
        ),
        body: FutureBuilder(
            future: CommonUtils.getUserInfo(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                var data = convert.jsonDecode(snapshot.data);
                widget.userModel = UserModel.fromJson(data);
                return Column(
                  children: <Widget>[
                    MemberHeader(
                      userModel: widget.userModel,
                    ),
                    Container(height: 8.0, color: Colors.black12),
                    IconTile(
                        leading: Icons.people,
                        title: "完善资料",
                        action: () {
                          var page = ProfileDetailPage();
                          Navigator.push(
                              context,
                              new MaterialPageRoute(
                                  builder: (context) => page));
                        }),
                    Divider(height: 2.0, color: Colors.black38),
                    IconTile(
                        leading: Icons.verified_user,
                        title: "党员认证",
                        action: () {
                          var page = ProfileVerifyDetailPage();
                          Navigator.push(
                              context,
                              new MaterialPageRoute(
                                  builder: (context) => page));
                        }),
                    Divider(height: 2.0, color: Colors.black38),
//                    IconTile(
//                        leading: Icons.view_compact,
//                        title: "关于阳光北亭",
//                        action: () {}),
//                    Divider(height: 2.0, color: Colors.black38),
                  ],
                );
              } else {
                return Column(children: <Widget>[
                  IconTile(
                      leading: Icons.view_compact,
                      title: "关于阳光北亭",
                      action: () {}),
                  Divider(height: 2.0, color: Colors.black38),
                ]);
              }
            }),
        bottomSheet: initBottom(),
      ),
    );
  }

  Widget initBottom() {
    return BottomAppBar(
        child: Container(
            height: 60,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Expanded(
                    child: JdButton(

                  callBack: () {
                    showAlertDialog(context);
                  },
                  text: "退出登录",
                  color: Colors.red,
                )),
              ],
            )));
  }

  void showAlertDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (_) => new AlertDialog(
                title: new Text("是否退出登录"),
                content: new Text(""),
                actions: <Widget>[
                  new FlatButton(
                    child: new Text("取消"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  new FlatButton(
                    child: new Text("确定"),
                    onPressed: () {
                      SharedPreferenceUtil.remove(
                          SharedPreferenceUtil.KEY_USERMODEL);
                      sp.remove(K_USER_ID);
                      Navigator.of(context).pushNamedAndRemoveUntil(
                          "/login", ModalRoute.withName("/login"));
                    },
                  )
                ]));
  }
}
